var costRatioData = [];
var balanceInitSettingData = undefined;
var flagDate = undefined;
var BalanceMonth = undefined;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("costRatioThead", costRatioTable);
	$ajaxList('searchBalanceInitSetting', getBalanceInitSetting);
    setTimeout(hideLoading, 700);
});
function getBalanceInitSetting(data){
	balanceInitSettingData = data.balanceInitSettingList[0]
	$("#BalanceChargingPriceType").val(balanceInitSettingData.balanceChargingPriceType);
	var balanceStartMonth = balanceInitSettingData.balanceStartMonth
	changeYear(0, balanceStartMonth);
	showMonth(getSystemTime().getMonth() + 1);
}
function getBalanceBillName(data) {
	BalanceBillNameList = data || [];
	$("#BalanceBillID").empty();
	$("#BalanceBillID").append("<option value='' selected='selected'>请选择</option>");
	$("#ChargingPrice").text('');
	var length = data.length;
	if (length == 0) {
		$("#ChargingPrice").text(messageContent["Balance.MonthBalanceBillNotExist"]);
	} else {
		for (var i = 0; i < length; i++) {
			var costRatioCreate = "";
			if (data[i].createFlag) {
				costRatioCreate = '(系数未生成)';
			} 
//			else {
//				costRatioCreate = '(系数已生成)';
//			}
			$("#BalanceBillID").append("<option value='" + data[i].balanceBillID + "'>" + (data[i].balanceBillName + costRatioCreate) + "</option>");
		}
		if (!$("#BalanceBillID").val()) {
			$("#BalanceBillID").tips({
				side : 2,
				msg : "请选择电力结算单",
				bg : '#AE81FF',
				time : 1
			});
			$("#BalanceBillID").focus();
		}
	}
}
function showMonth(index){
	$('.activeMonthLi').removeClass("activeMonthLi");
	$("#li"+ index).addClass("activeMonthLi");
	var year = $("#showYear").html().replace(/[^0-9]/ig,"");
	index = index >= 10 ? index : "0" + index;
	$("#balanceMonth").val(year + index);
	$ajaxList('balanceBillName', getBalanceBillName, {BalanceMonth: $("#balanceMonth").val()});
	$("#update").hide();
	$("#create").hide();
	$("#ChargingPrice").html("");
	$("#tradeKWHTotal").html("");
	$("#chargingKWHTotal").html("");
	$("#chargingStationRatioTotal").html("");
/*	$("#fixedRatio").val("");
	$("#fixedRatio").attr("readonly", true);*/
	clearTable("costRatioTbody");
}

function changeBill(index){
	$("#update").hide();
	$("#tradeKWHTotal").html("");
	$("#chargingKWHTotal").html("");
	$("#chargingStationRatioTotal").html("");
	$("#costRatioTbody").html("");
	$("#ChargingPrice").html("");
/*	$("#fixedRatio").val("");
	$("#fixedRatio").attr("readonly", true);*/
	if (!$("#BalanceBillID").val()) {
		$("#BalanceBillID").tips({
			side : 2,
			msg : "请选择电力结算单",
			bg : '#AE81FF',
			time : 1
		});
		$("#BalanceBillID").focus();
		return;
	}
	var balanceChargingPriceType = BalanceBillNameList[index-1] && BalanceBillNameList[index-1].balanceChargingPriceType;
	if (balanceChargingPriceType == '1') {
		$("#BalanceChargingPriceType").val(balanceChargingPriceType);
		$("#ChargingPrice").text('固定电价：' + BalanceBillNameList[index-1].chargingFixedPrice + '元');
	} else if (balanceChargingPriceType == '2') {
		$("#BalanceChargingPriceType").val(balanceChargingPriceType);
		$("#ChargingPrice").text('平谷电价：' + '尖:' + BalanceBillNameList[index-1].chargingSharpPrice +'元， 峰:' + BalanceBillNameList[index-1].chargingPeakPrice + '元， 平:' + BalanceBillNameList[index-1].chargingFlatPrice + '元， 谷:' + BalanceBillNameList[index-1].chargingValleyPrice + '元');
	}
	if (BalanceBillNameList[index-1] && BalanceBillNameList[index-1].createFlag) {
		$("#create").show();
	} else {
		$("#create").hide();
		searchRatio();
	}
}

function saveRatio(){
	showMessge(messageContent["Balance.BalanceRatioConfirm"], "model2");
}
function getUrl(){
	return 'saveCostRatio';
}
function getData(){
	return $("#costRatioSearch").serialize();
}
function sure(data){
	var result = data && data.result;
    if (result == "Success") {
    	$("#update").hide();
    	$("#create").hide();
    /*	$("#fixedRatio").attr("readonly", true);*/
		$("#ChargingPrice").text('');
		$ajaxList('balanceBillName', getBalanceBillName, {BalanceMonth: $("#balanceMonth").val()});
		showMessge(messageContent["Balance.BalanceRatioSaveSuccess"]);
    } else if (result == "NotFound") {
    	showMessge(messageContent["Balance.BalanceRatioNotFound"]);
    } else {
    	showMessge(messageContent["Balance.BalanceRatioError"]);
    }
}

function createRatio(){
	var balanceBillID = $("#BalanceBillID").val();
	if (!$("#BalanceBillID").val()) {
		$("#BalanceBillID").tips({
			side : 2,
			msg : "请选择电力结算单",
			bg : '#AE81FF',
			time : 1
		});
		$("#BalanceBillID").focus();
		return;
	}
	$ajax('createCostRatio', 'costRatioSearch', createCostRatio);
}
function searchRatio(){
	$ajax('searchCostRatio', 'costRatioSearch', searchCostRatio);
}
function searchCostRatio(data){
	costRatioData = data && data.costRatioList || [];
	var length = costRatioData.length;
	if (length == 0) {
		$ajaxList('balanceBillName', getBalanceBillName, {BalanceMonth: $("#balanceMonth").val()});
		showMessge(messageContent["Balance.BalanceRatioNotExist"]);
		return;
	}
	var costFixedRatioData = [];
	for(var i=0; i<length; i++){
		if (costRatioData[i].balanceRatioType == '1') {
			$("#fixedRatio").html(costRatioData[i].balanceRatio);
		} else if (costRatioData[i].balanceRatioType == '2') {
			$("#tradeKWHTotal").html(costRatioData[i].tradeKWH);
			$("#chargingKWHTotal").html(costRatioData[i].chargingKWH);
			$("#chargingStationRatioTotal").html(costRatioData[i].balanceRatio);
		} else {
			costFixedRatioData.push(costRatioData[i]);
		}
	}
	createTableBody("costRatioTbody", costRatioTable, costFixedRatioData, true, true);
}
function createCostRatio(data){
	costRatioData = data && data.costRatioList || [];
	var length = costRatioData.length;
	if (length == 0) {
		$ajaxList('balanceBillName', getBalanceBillName, {BalanceMonth: $("#balanceMonth").val()});
		$("#create").hide();
		$("#update").hide();
		showMessge(messageContent["Balance.BalanceRatioNotExist"]);
		return;
	}
	$("#create").hide();
	$("#update").show();
	if (!$("#fixedRatio").html()) {
		$("#fixedRatio").html(balanceInitSettingData.defaultFixedRatio);
		$("#FixedRatio").val(balanceInitSettingData.defaultFixedRatio);
	}
	createTableBody("costRatioTbody", costRatioTable, costRatioData, true, true);
	var tradeKWHTotal = 0; // 总贸易电量
	var chargingKWHTotal = 0; // 总充电量
	var chargingStationRatioTotal = 0;
	for(var i=0; i<length; i++){
		tradeKWHTotal += costRatioData[i].tradeKWH;
		chargingKWHTotal += costRatioData[i].chargingKWH;
	}
	chargingStationRatioTotal = tradeKWHTotal/chargingKWHTotal;
	$("#tradeKWHTotal").html(tradeKWHTotal);
	$("#chargingKWHTotal").html(chargingKWHTotal);
	$("#chargingStationRatioTotal").html(chargingStationRatioTotal);
}